#pragma once

using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Drawing;

#include "UtilLoader.h"
#include "UtilManager.h"

namespace ttg_internal
{
	namespace utils
	{
		/// <summary>
		/// Summary for UtilityChooser
		///
		/// WARNING: If you change the name of this class, you will need to change the
		///          'Resource File Name' property for the managed resource compiler tool
		///          associated with all .resx files this class depends on.  Otherwise,
		///          the designers will not be able to interact properly with localized
		///          resources associated with this form.
		/// </summary>
		public ref class UtilityChooser : public System::Windows::Forms::Form
		{
		public:
			UtilityChooser(void)
			{
				InitializeComponent();
				lbUtilities->Items->AddRange(
					ttg_internal::utils::UtilLoader::findAvailableUtilities()->ToArray());
			}

		protected:
			/// <summary>
			/// Clean up any resources being used.
			/// </summary>
			~UtilityChooser()
			{
				if (components)
				{
					delete components;
				}
			}
		private: System::Windows::Forms::ListBox^  lbUtilities;
		protected: 

		private: System::Windows::Forms::Button^  btLoad;
		private: System::Windows::Forms::Button^  btImport;



		protected: 

		private:
			/// <summary>
			/// Required designer variable.
			/// </summary>
			System::ComponentModel::Container ^components;

	#pragma region Windows Form Designer generated code
			/// <summary>
			/// Required method for Designer support - do not modify
			/// the contents of this method with the code editor.
			/// </summary>
			void InitializeComponent(void)
			{
				this->lbUtilities = (gcnew System::Windows::Forms::ListBox());
				this->btLoad = (gcnew System::Windows::Forms::Button());
				this->btImport = (gcnew System::Windows::Forms::Button());
				this->SuspendLayout();
				// 
				// lbUtilities
				// 
				this->lbUtilities->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
					| System::Windows::Forms::AnchorStyles::Left) 
					| System::Windows::Forms::AnchorStyles::Right));
				this->lbUtilities->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
					static_cast<System::Byte>(204)));
				this->lbUtilities->FormattingEnabled = true;
				this->lbUtilities->ItemHeight = 15;
				this->lbUtilities->Location = System::Drawing::Point(12, 12);
				this->lbUtilities->Name = L"lbUtilities";
				this->lbUtilities->Size = System::Drawing::Size(260, 94);
				this->lbUtilities->TabIndex = 0;
				this->lbUtilities->DoubleClick += gcnew System::EventHandler(this, &UtilityChooser::listBox1_SelectedIndexChanged);
				// 
				// btLoad
				// 
				this->btLoad->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
				this->btLoad->Location = System::Drawing::Point(116, 127);
				this->btLoad->Name = L"btLoad";
				this->btLoad->Size = System::Drawing::Size(75, 23);
				this->btLoad->TabIndex = 1;
				this->btLoad->Text = L"Load";
				this->btLoad->UseVisualStyleBackColor = true;
				this->btLoad->Click += gcnew System::EventHandler(this, &UtilityChooser::btLoad_Click);
				// 
				// btImport
				// 
				this->btImport->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
				this->btImport->Enabled = false;
				this->btImport->Location = System::Drawing::Point(197, 127);
				this->btImport->Name = L"btImport";
				this->btImport->Size = System::Drawing::Size(75, 23);
				this->btImport->TabIndex = 2;
				this->btImport->Text = L"Import ...";
				this->btImport->UseVisualStyleBackColor = true;
				this->btImport->Click += gcnew System::EventHandler(this, &UtilityChooser::btImport_Click);
				// 
				// UtilityChooser
				// 
				this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
				this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
				this->ClientSize = System::Drawing::Size(284, 162);
				this->Controls->Add(this->btImport);
				this->Controls->Add(this->btLoad);
				this->Controls->Add(this->lbUtilities);
				this->MinimumSize = System::Drawing::Size(300, 200);
				this->Name = L"UtilityChooser";
				this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
				this->Text = L"UtilityChooser";
				this->ResumeLayout(false);

			}
	#pragma endregion
			private:
				void TryLoadUtility()
				{
					if (lbUtilities->SelectedItem != nullptr)
					 {
						 UtilManager::Init(
							 gcnew UtilityProps(
								safe_cast<System::String ^>(lbUtilities->SelectedItem)));
						 this->Close();
					 }
				}
		private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e)
				 { TryLoadUtility(); }
		private: System::Void btLoad_Click(System::Object^  sender, System::EventArgs^  e)
				 { TryLoadUtility(); }
			private:
				System::Void btImport_Click(System::Object^  sender, System::EventArgs ^e)
				{
					//Getting name of file with utility.
					/*System::Windows::Forms::OpenFileDialog ^ofd
						= gcnew System::Windows::Forms::OpenFileDialog();
					ofd->Filter = "Utilities|*.dll";
					System::Windows::Forms::DialogResult res = ofd->ShowDialog();
					if (res == System::Windows::Forms::DialogResult::OK)
					{
						System::String ^err;
						IUtility ^res
							= UtilLoader::LoadUtilityFromFile(ofd->FileName, err);
						if (res != nullptr)
							err = UtilManager::Register(res);
						if (err != nullptr)
							System::Windows::Forms::MessageBox::Show(err, "Cannot load utility");
					}*/
				}
};
	}
}
